package com.sgb.kjwl.view.ui.adapter.manage;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemCommendedEnterpriseMainBinding;
import com.sgb.kjwl.databinding.ItemOneVideoBinding;
import com.sgb.kjwl.databinding.ItemVideoBinding;
import com.sgb.kjwl.interfaces.RecyclerClick;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfAndVideoEntity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.transport.CarHallEntity;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.swgk.core.util.UserOperating;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.swgk.core.dialog.RoundDialog;

import java.io.Serializable;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 3/3/21 8:10 PM
 * @Description: 车辆大厅适配器
 */
public class ManageHallAdapter extends BaseMultiItemQuickAdapter<TheEnterpriseOfAndVideoEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    private RecyclerClick<TheEnterpriseOfAndVideoEntity> mRecyclerClick;

    public RecyclerClick<TheEnterpriseOfAndVideoEntity> getmRecyclerClick() {
        return mRecyclerClick;
    }

    public void setmRecyclerClick(RecyclerClick<TheEnterpriseOfAndVideoEntity> mRecyclerClick) {
        this.mRecyclerClick = mRecyclerClick;
    }

    public ManageHallAdapter(List<TheEnterpriseOfAndVideoEntity> data) {
        super(data);
        //车辆类别
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_commended_enterprise_main);
        //视频类别
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_video_list);
        //单个视频类别
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.item_one_video);
    }

    @Override
    protected void convert(BaseViewHolder helper, TheEnterpriseOfAndVideoEntity item) {
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                initCarInfo(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                initVideoList(helper, item);
                break;
            case SingleItemConstant.TYPE_THREE:
                initSingleVideo(helper, item);
                break;
        }
    }

    /**
     * 设置车辆信息
     */
    private void initCarInfo(BaseViewHolder helper, TheEnterpriseOfAndVideoEntity entity) {
        //设置dataBinding数据
        ItemCommendedEnterpriseMainBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        binding.setVariable(BR.viewModel, entity);
        binding.executePendingBindings();
        if (entity == null) {
            LogUtil.d(TAG, "RecommendedEnterprisesAdapter entity is null");
            return;
        }
        binding.tvLine.setVisibility(View.VISIBLE);
        if (entity.getStaffNumber() == null || entity.getStaffNumber().equals("")) {
            binding.tvLine.setVisibility(View.GONE);
            binding.tvCount.setVisibility(View.GONE);
        } else {
            binding.tvCount.setText(entity.getStaffNumber() + "人");
            binding.tvCount.setVisibility(View.VISIBLE);
        }
        if (entity.getServiceType() == null || entity.getServiceType().equals("")) {
            binding.tvLine.setVisibility(View.GONE);
            binding.tvServiceType.setVisibility(View.GONE);
        } else {
            binding.tvServiceType.setText(entity.getServiceType());
            binding.tvServiceType.setVisibility(View.VISIBLE);
        }

        if (entity.getLatitude()!=null&&entity.getLongitude()!=null&&entity.getLatitude().equals("0") && entity.getLongitude().equals("0")) {//经纬度为0
            if (entity.getAddressNew() != null && !entity.getAddressNew().equals("")) {//地址不为空
                binding.tvAddress.setVisibility(View.VISIBLE);
            } else {
                binding.tvAddress.setVisibility(View.GONE);
            }
            binding.tvDistance.setVisibility(View.GONE);
        } else {
            if (entity.getAddressNew() != null && !entity.getAddressNew().equals("")) {//地址不为空
                binding.tvAddress.setVisibility(View.VISIBLE);
                binding.tvDistance.setVisibility(View.VISIBLE);
            } else {
                binding.tvAddress.setVisibility(View.GONE);
                binding.tvDistance.setVisibility(View.GONE);
            }
        }

        binding.tvFollow.setBackgroundResource(entity.getFollow()?R.drawable.corner5_969696_bg:R.drawable.corner5_ef4033_bg);
        binding.tvFollow.setText(entity.getFollow()?"已关注":"关注");
        binding.tvFollow.setTextColor(entity.getFollow()?mContext.getResources().getColor(R.color.color_969696):mContext.getResources().getColor(R.color.color_main));

        TextView tv = helper.getView(R.id.tv_follow);
        tv.setOnClickListener(v -> {
            if (mRecyclerClick != null){
                mRecyclerClick.followClick(entity);
            }
        });
    }

    /**
     * 设置视频信息
     */
    private void initVideoList(BaseViewHolder helper, TheEnterpriseOfAndVideoEntity entity) {
        RecyclerView recyclerView = helper.getView(R.id.rv_item_video_list);
        CarsHallVideoAdapter adapter = new CarsHallVideoAdapter(entity.getList());
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
//        if (recyclerView.getItemDecorationCount() == 0) {
//            DividerDecoration.builder(mContext)
//                    .color(Color.TRANSPARENT)
//                    .size(10, TypedValue.COMPLEX_UNIT_DIP)
//                    .build()
//                    .addTo(recyclerView);
//        }
        adapter.bindToRecyclerView(recyclerView);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            //视频播放
            Intent intent = new Intent(mContext, PlayListActivity.class);
            intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) entity.getList());
            intent.putExtra(PlayListActivity.PSO, position);
            intent.putExtra(PlayListActivity.VIDEOTYPE, "goods_and_car_video");
            mContext.startActivity(intent);
        });
    }

    /**
     * 设置单个视频信息
     */
    private void initSingleVideo(BaseViewHolder helper, TheEnterpriseOfAndVideoEntity entity) {
        //设置dataBinding数据
        ItemOneVideoBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        if (entity.getList() != null && !entity.getList().isEmpty()) {
            binding.setVariable(BR.video, entity.getList().get(0));
            binding.executePendingBindings();
        }
    }

    private void telClick(CarHallEntity entity) {
        //检查登录状态
        if (UserOperating.getInstance().isNotLogged(mContext)) {
            return;
        }
        if (!TextUtils.isEmpty(entity.getTelphone())) {
            showTelDialog(entity.getTelphone());
        }
    }

    private void showTelDialog(String phone) {
        new RoundDialog.Builder(mContext)
                .title("确认呼叫")
                .content(phone)
                .confirm("拨打")
                .cancelable(false)
                .confirmCallback(v -> {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent.setData(data);
                    mContext.startActivity(intent);
                })
                .show();
    }

    /**
     * 车辆大厅视频适配器
     */
    private static class CarsHallVideoAdapter extends BaseQuickAdapter<VideoBean, BaseViewHolder> {

        public CarsHallVideoAdapter(@Nullable List<VideoBean> data) {
            super(R.layout.item_video, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, VideoBean entity) {
            if (entity.getCover() != null && entity.getCover().endsWith(".")){
                entity.setCover(entity.getCover()+"jpg");
            }
            //设置dataBinding数据
            ItemVideoBinding binding = DataBindingUtil.bind(helper.itemView);
            if (binding == null) return;
            binding.setVariable(BR.video, entity);
            binding.executePendingBindings();
        }
    }
}
